import { createRouter, createWebHistory } from 'vue-router';

import AuthCallback from '../views/AuthCallback.vue';
import UserManager from '../components/UserManager.vue'
import LoginView from '../views/LoginView.vue'
import MainLayout from '../layouts/MainLayout.vue'
import Dashboard from '../components/Dashboard.vue'
import RoleManager from '@/components/RoleManager.vue';
import PermissionManager from '@/components/PermissionManager.vue'; 
import InterfaceManager from '@/components/InterfaceManager.vue';
import LogManager from '@/components/LogManager.vue';
import Systemsettings from '@/components/Systemsettings.vue';
import DataManager from '@/components/DataManager.vue';
import ResourceManager from '@/components/ResourceManager.vue';
import UserProfile from '@/components/UserProfile.vue';
import path from 'path';

const routes = [

  { path: '/auth-callback', name: 'AuthCallback', component: AuthCallback },
  { path: '/login', name: 'Login', component: LoginView },
  {
    path: '/', name: 'Main', component: MainLayout, redirect: { name: 'Dashboard' }, // 关键点
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: Dashboard
      },
      {
        path: 'user-manager',
        name: 'UserManager',
        component: UserManager
      },
      {
        path: 'role-manager',
        name: 'RoleManager',
        component: RoleManager
      },
      {
        path:'permission-manager',
        name: 'PermissionManager',
        component: PermissionManager
      },
      {
        path:'interface-manager',
        name: 'InterfaceManager',
        component: InterfaceManager
      },
      {
        path:'log-manager',
        name: 'LogManager',
        component: LogManager
      },
      {
        path:'system-settings',
        name: 'Systemsettings',
        component: Systemsettings
      },
      {
        path:'data-manager',
        name: 'DataManager',
        component: DataManager
      },
      {
        path:'resource-manager',
        name: 'ResourceManager',
        component: ResourceManager
      },
      {
        path:'user-profile',
        name: 'UserProfile',
        component: UserProfile
      }

    ]
  }
];


const router = createRouter({
  history: createWebHistory(),
  routes
})


router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  const allowAnonymous = ['/login', '/auth-callback'];

  if (!token && !allowAnonymous.includes(to.path)) {
    next('/login');
  } else {
    next();
  }
})

export default router